<template>
    <div>
        <div class="page-title">录入信息</div>
        <el-form ref="form" :model="form" label-width="200px" :inline="true">
            <el-form-item label="供货公司.">
              <el-select v-model="form.ghgs" placeholder="供货公司">
                <el-option label="请选择" value="0"></el-option>
                <el-option
                    v-for="item in supplycompany"
                    :key="item.id"
                    :label="item.company_name"
                    :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="采购合同">
                <el-upload
                class="upload-demo"
                :action="uploadUrl"
                :on-preview="handlePreview"
                :on-success="handleUploadSuccess"
                :before-remove="beforeRemove"
                :multiple = true
                :file-list="fileList"
                list-type="picture"
                > <el-button size="small" type="primary">点击上传</el-button>
                </el-upload>
            </el-form-item>
            <br>
            <el-form-item label="采购单价（元）">
                <el-input ref="sort" v-model="form.cgdj" ></el-input>
            </el-form-item>
            
            <el-form-item label="采购数量（吨）">
                <el-input ref="sort" v-model="form.cgsl" ></el-input>
            </el-form-item>
            
            <el-form-item label="采购价款（元）">
                <el-input ref="sort" v-model="form.cgjk" ></el-input>
            </el-form-item>
            
            <el-form-item label="供货时间">
              <el-date-picker
                  v-model="form.fksj"
                  type="date"
                  format="YYYY 年 MM 月 DD 日"
                  value-format="YYYY-MM-DD HH:mm:ss"
                  placeholder="选择日期">
              </el-date-picker>
            </el-form-item>

            <el-form-item label="收货公司">
                <el-select v-model="form.shdw" placeholder="收货公司">
                    <el-option label="请选择" value="0"></el-option>
                    <el-option
                        v-for="item in receivecompany"
                        :key="item.id"
                        :label="item.receive_company_name"
                        :value="item.id"
                    ></el-option>
                </el-select>
            </el-form-item>
            
            <el-form-item label="销售合同">
                <el-upload
                class="upload-demo"
                :action="uploadUrl"
                :on-preview="handlePreview"
                :on-success="handleUploadSuccess2"
                :before-remove="beforeRemove"
                :multiple = true
                :file-list="fileList"
                list-type="picture"
                > <el-button size="small" type="primary">点击上传</el-button>
                </el-upload>
            </el-form-item>
            
            <br>

            <el-form-item label="销售单价（元）">
                <el-input ref="sort" v-model="form.xsdj" ></el-input>
            </el-form-item>
            
            <el-form-item label="销售数量（吨）">
                <el-input ref="sort" v-model="form.xssl" ></el-input>
            </el-form-item>
            
            <el-form-item label="销售价款（元）">
                <el-input ref="sort" v-model="form.xsjk" ></el-input>
            </el-form-item>
            
            <el-form-item label="销售时间">
              <el-date-picker
                  v-model="form.sksj"
                  type="date"
                  format="YYYY 年 MM 月 DD 日"
                  value-format="YYYY-MM-DD"
                  placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
            
            <el-form-item label="终端用户">
              <el-select v-model="form.zdyh" placeholder="用户名">
                <el-option label="请选择" value="0"></el-option>
                <el-option
                    v-for="item in enduserlist"
                    :key="item.id"
                    :label="item.user_name"
                    :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            
            <el-form-item label="图片上传">
                <el-upload
                    class="upload-demo"
                    :action="uploadUrl"
                    :on-preview="handlePreview"
                    :on-success="handleUploadSuccess3"
                    :on-remove="handleRemove"
                    :file-list="fileList"
                    :multiple = true
                    list-type="picture">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <template v-slot:tip class="el-upload__tip"><br/>只能上传jpg/png文件，且不超过500kb</template>
                </el-upload>
            </el-form-item>
            <el-form-item label="品名">
                <el-select
                        v-model="form.value"
                        filterable
                        allow-create
                        default-first-option
                        placeholder="请选择品名">
                    <el-option
                            v-for="item in productlist"
                            :key="item.id"
                            :label="item.product_name"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <br>
            <el-form-item>
                <el-button type="success" icon="el-icon-check" @click="submit">添加</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import {
  request_company_read,
  request_create_log,
  request_receive_read,
  request_product_read,
  request_edit_log, request_end_read
} from '@/network/api';
    import {
        __URL__
    } from '@/main.js'


    export default {
        name: 'addMessage',
        data() {
            return {
                uploadUrl:__URL__ + '/uploadImage',
                form: {
                    files:[],
                    cght:[],
                    xsht:[],
                    value: [],

                },
              supplycompany:[],
              receivecompany:[],
              productlist:[],
              enduserlist:[],
            }
        },
        mounted() {
          if(this.$route.query.arr){
            this.form = JSON.parse(this.$route.query.arr);
          }
          this.form.status = true
          this.getSupplyCompany()
          this.getReceiveCompany()
          this.getProductName()
          this.getEndUser()
        },
        methods: {
          getSupplyCompany(){
            request_company_read().then((res)=>{
              let result = res.data;
              if(result.code == 200){
                  this.supplycompany = result.data
              }
            }).catch()
          },
          getReceiveCompany(){
            request_receive_read().then((res)=>{
              let result = res.data;
              if(result.code == 200){
                this.receivecompany = result.data
              }
            }).catch()
          },
          getProductName(){
            request_product_read().then((res)=>{
              let result = res.data;
              if(result.code == 200){
                this.productlist = result.data
              }
            }).catch()
          },
          getEndUser(){
            request_end_read().then((res)=>{
              let result = res.data;
              if(result.code == 200){
                this.enduserlist = result.data
              }
            }).catch()
          },
            handleUploadSuccess(res) {
                this.form.cght.push(res.data)
            },
            handleUploadSuccess2(res) {
                this.form.xsht.push(res.data)
            },
            handleUploadSuccess3(res) {
                this.form.files.push(res.data)
            },
            // 提交
            submit() {
              if(this.form.id){
                request_edit_log({
                  ...this.form
                }).then(res => {
                  const result = res.data
                  if (result.code == 200) {
                    this.$message.success({
                      title: '系统提示',
                      message: '操作成功',
                      offset: 70
                    })
                    this.$router.push('/home')
                  }
                }).catch()
              }else{
                request_create_log({
                  ...this.form
                }).then(res => {
                  const result = res.data
                  if (result.code == 200) {
                    this.$message.success({
                      title: '系统提示',
                      message: '操作成功',
                      offset: 70
                    })
                    this.$router.push('/home')
                  }
                }).catch()
              }
              },
            // 取消
            cancel() {
                this.$router.push('/partyType')
            }
        }
    }
</script>

<style>
</style>